package com.study.ChantGpt;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangyanbin
 * @since 20221209
 **/
public class TreeNode {

    private int data;
    private List<TreeNode> children;

    public TreeNode(int data) {
        this.data = data;
        this.children = new ArrayList<>();
    }

    public int getData() {
        return data;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public TreeNode convertListToTree(List<Integer> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        // 取出列表中的第一个元素作为根节点
        int rootValue = list.get(0);
        TreeNode root = new TreeNode(rootValue);
        // 取出列表中除根节点外的剩余部分作为子列表
        List<Integer> subList = list.subList(1, list.size());
        // 递归
        // 递归转换子列表
        for (Integer value : subList) {
            root.getChildren().add(convertListToTree(subList));
        }
        return root;
    }

    @Test
    public void convertListToTree2() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        TreeNode treeNode = convertListToTree(list);
        System.out.println(treeNode);


    }


}
